﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm_2.Easy_290_WordPattern
{
    class MainProject
    {
        static void Main(string[] args)
        {
            int[] intArray = new int[] { -2, 0, 3, -5, 2, -1 };
            //Console.WriteLine(WordPattern("abba","dog cat cat dog"));
            //Console.WriteLine(WordPattern("abba","dog cat cat fish"));
            Console.WriteLine(WordPattern("abba", "dog dog dog dog"));
            Console.ReadKey();
        }

        public static bool WordPattern(string pattern, string s)
        {
            Dictionary<char, string> dic = new Dictionary<char, string>();
            string[] strs = s.Split(' ');
            if (pattern.Length != strs.Length) return false;

            int len = pattern.Length;
            for (int i = 0; i < len; i++)
            {
                if (dic.ContainsKey(pattern[i]))
                {
                    if (dic[pattern[i]] != strs[i]) return false;
                }
                else
                {
                    foreach (var item in dic)
                    {
                        if (item.Value == strs[i])
                        {
                            if (item.Key != pattern[i])
                            {
                                return false;
                            }
                        }
                    }
                    dic.Add(pattern[i], strs[i]);
                }
            }
            return true;
        }
    }
}
